package com.tianpengcc.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tianpengcc.model.DateType;
import com.tianpengcc.model.Diray;
import com.tianpengcc.model.DirayType;
import com.tianpengcc.model.custom.DirayTypeCustom;
import com.tianpengcc.service.DirayService;
import com.tianpengcc.service.DirayTypeService;
import com.tianpengcc.service.impl.DirayServiceImpl;
import com.tianpengcc.service.impl.DirayTypeServiceImpl;
import com.tianpengcc.util.PropertiesUtil;

public class DirayServlet extends HttpServlet {
	
	private DirayService dirayService = new DirayServiceImpl() ;
	
	private DirayTypeService dirayTypeService = new DirayTypeServiceImpl() ;
	
	/**
	 * <p>Description:</p>
	 * <p>Compary</p>
	 * @author 田鹏
	 * @date 2017年10月26日下午11:46:48
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response) ;
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String method = request.getParameter("method");
			String resultPath = "" ;
			if("show".equals(method)){
				resultPath = show(request,response);
			}else if("editUI".equals(method)){
				resultPath = editUI(request,response) ;
			}else if("editOrSave".equals(method)){
				resultPath = editOrSave(request,response) ;
			}else if("delete".equals(method)){
				resultPath = delete(request,response) ;
			}
			
			List<DirayTypeCustom> dirayTypeList = dirayTypeService.getAll();
			List<DateType> dateTypeList = dirayService.getDateTypeBySize(Integer.parseInt(PropertiesUtil.getValue("dateTypeSize")));
			
			String[] actionMethod = resultPath.split(":");
			if("dispatcher".equals(actionMethod[0])){
				request.setAttribute("mainPage", actionMethod[1]);
				request.setAttribute("dateTypeList", dateTypeList);
				request.setAttribute("dirayTypeList", dirayTypeList);
				request.getRequestDispatcher("/main.jsp").forward(request, response) ;
			}else{
				response.sendRedirect(request.getContextPath() + actionMethod[1]);
			}
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("error", "多半是错误了！");
			request.getRequestDispatcher("/error.jsp").forward(request, response);
		}
	}

	public String show(HttpServletRequest request, HttpServletResponse response)
		throws Exception{
		String dirayId = request.getParameter("dirayId");
		Diray diray = dirayService.getByDirayId(Integer.parseInt(dirayId));
		request.setAttribute("diray", diray);
		
		return "dispatcher:/diray/dirayShow.jsp" ;
		
	}
	
	public String editUI(HttpServletRequest request, HttpServletResponse response)
			throws Exception{
		String dirayId = request.getParameter("dirayId");
		Diray diray = null ;
		if(dirayId != null && !"".equals(dirayId)){
			diray = dirayService.getByDirayId(Integer.parseInt(dirayId)) ;
		}
		
		request.setAttribute("diray", diray);
		return "dispatcher:/diray/dirayEdit.jsp" ;
	}
	
	public String editOrSave(HttpServletRequest request, HttpServletResponse response)
			throws Exception{
		String dirayId = request.getParameter("dirayId");
		String dirayName = request.getParameter("title") ;
		String content = request.getParameter("content") ;
		String dirayTypeId = request.getParameter("typeId") ;
		Diray new_diray = null ;
		if(dirayId == null || "".equals(dirayId)){
			//保存操作
			Diray diray = new Diray() ;
			diray.setDirayName(dirayName);
			diray.setContent(content);
			DirayType dirayType = new DirayType() ;
			dirayType.setDirayTypeId(Integer.parseInt(dirayTypeId));
			diray.setDirayType(dirayType);
			new_diray = dirayService.doAdd(diray) ;
		}else{
			Diray diray = new Diray() ;
			diray.setDirayId(Integer.parseInt(dirayId));
			diray.setDirayName(dirayName);
			diray.setContent(content);
			DirayType dirayType = new DirayType() ;
			dirayType.setDirayTypeId(Integer.parseInt(dirayTypeId));
			diray.setDirayType(dirayType);
			new_diray = dirayService.doUpdate(Integer.parseInt(dirayId), diray) ;
		}
		request.setAttribute("diray", new_diray);
		
		return "dispatcher:/diray/dirayShow.jsp" ;
	}
	
	public String delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		String dirayId = request.getParameter("dirayId");
		if(dirayId != null && !"".equals(dirayId)){
			dirayService.doDelete(Integer.parseInt(dirayId));
		}
		
		return "sendRedirect:/main";
	}
}
